#include <cstdio>
#include <unistd.h>
#include <cstdlib>
#include <cstring>
#include <cctype>

#include "common/dirclass.h"	//compile with dirclass.cpp
#include "common/dataconf.h"	//compile with dataconf.cpp
#include "common/syscomm.h"
#include "common/utils.h"
#include "common/version.h"	//string header
#include "common/generaldef.h"
#include "common/about.h"	//string header
#include "common/arguments.h"	//string header
#include "common/problem.h"	//no seperate cpp file with this.

using namespace std;
using namespace progutils;
using namespace fdutils;

dataconf_t dataconf;

const char defdataconf[]="dataconf.lsc";
void msgquit(const char *msg){
	printf("%s",msg);
	exit(0);
}

char *compe, *prob;
char defprefix[PATH_LEN];
int delay;

void parsearg(const int argc, char **argv){
	char curopt=0;
	opterr=0;
	while((curopt=getopt(argc, argv, "c:p:d:vha"))!=-1){
		switch(curopt){
			case 'c':
				compe=optarg;
				break;
			case 'p':
				prob=optarg;
				break;
			case 'd':
				if(!isdigit(optarg[0])) error("Illegal delay time.\n");
				delay=0;
				sscanf(optarg,"%d",&delay);
				break;
			case 'h':
				msgquit(argmsg);
			case 'v':
				msgquit(vermsg);
			case 'a':
				msgquit(abtmsg);
			case '?':
				if(optopt=='c' || optopt=='p' || optopt=='d')
					printf("Option -%c needs an argument.\n",optopt);
				else
					printf("Unknown option -%c\n",optopt);
				exit(1);
				break;
			default:
				printf("Error while processing options.\n");
				abort();
		}
	}
	if(argc-optind<=0){
		printf("Warning: ./dataconf.lsc is used as default.\n");
		if(!dataconf.getfile(defdataconf))
			error("Failed to open data configuration.\n");
		sprintf(defprefix,".");
	}
	else if(argc-optind>1) error("More than one dataconf file is given.\n");
	else{
		if(!dataconf.getfile(argv[optind]))
			error("Failed to open data configuration.\n");
		getdirpath(defprefix,argv[optind]);
		/* All the following file operations are redirected to the directory _defprefix_. */
	}
	/* Notice: dataconf opened here. */
}

char srcpath[PATH_LEN]="src", datapath[PATH_LEN]="data";

void getdata(){
	printf("Getting problems...\n");
	relate(datapath,datapath,defprefix);
	while(dataconf.getnextprob(datapath)){
		/* Print problem header message. */
		printf("Problem %s",dataconf.problems[dataconf.problems.size()-1].title());
		if(dataconf.problems[dataconf.problems.size()-1].ifchecker())
			printf("(use checker)");

		while(dataconf.getnextcase(datapath));
		printf(": %d cases found.\n",dataconf.problems[dataconf.problems.size()-1].casecnt());
	}
}

void getcomp(){
	dir_t srcdir;
	relate(srcpath,srcpath,defprefix);
	srcdir.open(srcpath);
	srcdir.scanentries(NULL,0);
	for(int i=0; i<srcdir.subdircnt(); i++){
		if(strcmp(srcdir.subdir(i)->d_name,"..")==0 || strcmp(srcdir.subdir(i)->d_name,".")==0) continue;
		if(compe && strcmp(srcdir.subdir(i)->d_name,compe)) continue;
	}
}

void init(){
	compe=prob=NULL;
	delay=0;	/* Remember this! */
}

int main(int argc, char *argv[]){
	init();

	parsearg(argc, argv);

	getdata();
	getcomp();
	return 0;
}
